//index.js
//获取应用实例
const app = getApp()
var currPage = 0;
var totalPages = 0;

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    searchKey : "",
    count : 0,
    dreamList : [],
    isFromSearch: true,
    searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
    searchLoadingComplete: false  //“没有数据”的变量，默认false，隐藏
  },
  /**
   * 事件处理函数
   */
  //搜索
  searchFun: function () {
    currPage = 0
    //搜索梦境境列表
    var that = this;
    var result = null;
    //网络请求解析
    wx.request({
      url: 'https://api.jiemengshi.com/wechat/dream/record/search',
      data: {
        title: that.data.searchKey,
        start: currPage,
      },
      header: {
        'Cookie': 'JSESSIONID=' + wx.getStorageSync('sessionkey')
      },
      success: function (res) {
        console.log(res)
        result = res.data.data
        for (var i = 0; i < result.length; i++) {
          result[i].id = result[i].id;
          result[i].title = result[i].title;
          result[i].time = result[i].createTime;
          result[i].new = result[i].newReply;
        }
        //设置数据显示
        that.setData({
          count: res.data.total,
          dreamList: result
        })
      },
      fail: function () {
        wx.showToast({
          title: '请求失败',
        })
      }
    })
  },

  //关键字输入
  listenerSearchKeyInput : function(e){
    this.setData({
      searchKey : e.detail.value
    })
  },

  //个人信息页面跳转
  personalInfo : function(){
    wx.navigateTo({
      url: '../persionalPage/persionalPage'
    })
  },

  onShareAppMessage: function (res) {
    return {
      title: '解梦师',
      path: '/pages/mengjingku/mengjingku',
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '转发成功',
          icon: 'success',
          duration: 1200
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '转发失败',
          icon: 'loading',
          duration: 1200
        })
      }
    }
  },

  onLoad: function (options) {
    //显示转发
    wx.showShareMenu({
      withShareTicket: true
    })
    //获取用户信息
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

    this.loadDreamList()
  },

  //加载梦境列表
  loadDreamList:function(){
    //获取梦境列表
    var that = this;
    var result = null;
    //网络请求解析
    wx.request({
      url: 'https://api.jiemengshi.com/wechat/dream/record/list',
      data: {
        start: currPage,
      },
      header: {
        'Cookie': 'JSESSIONID=' + wx.getStorageSync('sessionkey')
      },
      success: function (res) {
        console.log(res)
        result = res.data.data
        for (var i = 0; i < result.length; i++) {
          result[i].id = result[i].id;
          result[i].title = result[i].title;
          result[i].time = result[i].createTime;
          result[i].new = result[i].newReply;
        }
        //设置数据显示
        that.data.isFromSearch? result=result : result = that.data.dreamList.concat(result)
        that.setData({
          dreamList: result,
        })
        if(currPage == 0){
          that.setData({
            count: res.data.total,
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '请求失败',
        })
      }
    })
  },

  //scroll到底端
  lower: function (e) {
    console.log(e)
    var totalPages = Math.ceil(this.data.count / 10);
    console.log(totalPages);
    if (currPage < totalPages - 1) {
      currPage = currPage + 1;
      this.loadDreamList()
      this.setData({
        isFromSearch: false,
        searchLoading: true
      })
    }
    else{
      this.setData({
        searchLoadingComplete: true, //把“没有数据”设为true，显示  
        searchLoading: false  //把"上拉加载"的变量设为false，隐藏  
      });  
    }
  },

  //刷新
  fresh: function(){
    this.loadDreamList();
    wx.showToast({
      title: '刷新成功',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
